/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package funcenter.db;

import funcenter.main.Usuario;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author arthur
 */
public class DB {

    public DB() {
        try {
            PreparedStatement criarTabela = Conexao.getInstance().getConexao().
                    prepareStatement("CREATE TABLE IF NOT EXISTS usuarios ("
                    + "id IDENTITY PRIMARY KEY,"
                    + "nome VARCHAR(100) NOT NULL,"
                    + "login VARCHAR(50) NOT NULL,"
                    + "email VARCHAR(100) NOT NULL,"
                    + "senha VARCHAR(50) NOT NULL"
                    + ");");

            criarTabela.execute();
            
            Usuario user = new Usuario("ADMIN", "ADMIN", "", "ADMIN");
            if(!this.usuarioExiste(user)){
                this.inserir(user);        
            }
        } catch(Exception ex){
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean inserir(Usuario user) {        
        try {
            PreparedStatement ps = Conexao.getInstance().getConexao().
                    prepareStatement("INSERT INTO usuarios (nome,login,email,senha) values (?,?,?,?)");
            ps.setString(1, user.getNome());
            ps.setString(2, user.getLogin());
            ps.setString(3, user.getEmail());
            ps.setString(4, user.getSenha());

            ps.execute();

            return true;
        } catch(Exception ex){
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean usuarioExiste(Usuario user) {
        try {
            String andEmail = "";
            if(!user.getEmail().equals("")){
                andEmail = " AND email=" + user.getEmail();
            }
            
            PreparedStatement ps = Conexao.getInstance().getConexao().
                    prepareStatement("SELECT * FROM usuarios WHERE login='" + user.getLogin() + "'" + andEmail);

            ResultSet resultset = ps.executeQuery();
            
            if(!resultset.next()){
                return true;
            }
        } catch(Exception ex){
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }
    
    public boolean logar(Usuario user) {
        try {
            PreparedStatement ps = Conexao.getInstance().getConexao().
                    prepareStatement("SELECT login,senha FROM usuarios WHERE login='" + user.getLogin() + "'");

            ResultSet resultset = ps.executeQuery();
            while(resultset.next()){
                if (user.getLogin().equals(resultset.getObject(1))) {
                    if (user.getSenha().equals(resultset.getObject(2))) {
                        System.out.println("Usuario conectado com exito.");
                        return true;
                    }
                }
            }
        } catch(Exception ex){
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }
}
